<?php
/**
 * Created by PhpStorm.
 * User: 10851
 * Date: 2017/9/20
 * Time: 22:17
 */
header('content-type:text/html;charset=utf-8');
require_once 'config/config.php';
require_once 'functions/common.func.php';
require_once 'functions/mysql.func.php';
require_once 'swiftmailer-master/lib/swift_required.php';
$link = connect3();
$table = 'cs_zhuce';
$act = $_REQUEST ['act'];
$username = $_REQUEST['username'];
$password =md5($_POST['password']);
switch ($act){
    case 'reg':
        //关闭事物自动提交
        mysqli_autocommit($link ,false);
        //得到当前时间
        $regTime = time();
        //得到邮箱
        $email = $_POST['email'];
        //生成一个token
        $token = md5($username.$password.$regTime);
        // 生成一个token 过期时间
        $token_exptime = $regTime+24*3600;
        $data = compact('username','password','email','regTime','token','token_exptime');
        $res = insert($link,$data,$table);
        //创建一个transport
        $transport = Swift_SmtpTransport::newInstance('smtp.sina.com',25);
        //账号名
        $transport->setUsername('niyao1994@sina.com');
        //密码
        $transport->setPassword('a5201314100');
        //创建一个邮件发送对象
        $mailer = Swift_Mailer::newInstance($transport);
        //邮件信息对象
        $message = Swift_Message::newInstance();
        //谁来发送
        $message->setFrom(array('niyao1994@sina.com'));
        //发送到你那里？
        $message->setTo($email);
        //设置主题
        $message->setSubject('注册账号激活邮件');
        $activeStr = "?act=active&username={$username}&token={$token}";
        $url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].$activeStr;
        $urlEncode = urlEncode($url);
        $emailBoy = <<<EOF
        欢迎{$username}使用账号激活功能，请点击链接激活账号：
        <a href="{$url}" target="_blank">{$urlEncode}</a><br/>
        (该链接24小时内有效)如果上面不是链接形式，请将地址复制到浏览器在访问。
          <b>关于天渝传媒</b>
EOF;
        $message->setBody($emailBoy,'text/html','utf-8');
        try{
            $res1 = $mailer->send($message);
            if($res && $res1){
                mysqli_commit($link);
                mysqli_autocommit($link,true);
                alertMes('注册成功,请激活','index.php');
            }else{
                alertMes('注册失败，请重新注册','index.php');
            }
        }catch (Swift_ConnectionException $e){

            die('邮件服务器错误:').$e->getMessage();
        }

       break;
    case 'active':
        $token = $_GET['token'];
        $username = mysqli_real_escape_string($link,$username);
        $query = "select id,token_exptime from {$table} WHERE username='{$username}'";
        $user = fetchOne($link,$query);
        if($user){
            $now = time();
            $token_exptime = $user['$token_exptime'];
            if ($now<$token_exptime){
                delete($link,$table,'username={$username}');
                alertMes('激活码过期，请重新注册','index.php');
            }else{
                $data = array('status'=>1);
                $res = update($link,$data,$table);
                if ($res){
                    alertMes('激活成功','index.php');
                }else{
                    alertMes('激活失败，请重新注册','index.php');
                }
            }

        }else{
            alertMes('激活失败，没有找到要激活的用户','index.php');
        }
        break;
    case 'login':
        $username=addslashes($username);
        $query="SELECT id,status FROM {$table} WHERE username='{$username}' AND password='{$password}'";
        $row=fetchOne($link, $query);
        if($row){
            if($row['status']==0){
                alertMes('请先到邮箱激活再来登陆','index.php');
            }else{
                alertMes('登陆成功，跳转到首页','cs_yg/layout-index.php');
            }
        }else{
            alertMes('用户名或密码错误，重新登陆','index.php');
        }
        break;
    default:
        die('非法操作');
        break;
}